package org.edu.eece262.taskshare;

import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class CreateGroup extends Activity {

	LinearLayout llContacts;
	GroupDBAdapter groupDBA;
	ContactsDBAdapter contactsDBA;
	Button bCreateNewGroup;
	Button bCancelNewGroup;
	TextView tvGroupName;
	public static final String PREFS_NAME = "TASKSHARELOGIN";

	LinkedList<Messages.GroupMember> newMembers;

	Intent results;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.creategroup);

		groupDBA = GroupDBAdapter.getGroupDBA(getApplicationContext());
		contactsDBA = ContactsDBAdapter
				.getContactsDBAdapter(getApplicationContext());

		bCreateNewGroup = (Button) findViewById(R.id.bCreateNewGroup);
		bCancelNewGroup = (Button) findViewById(R.id.bCancelGroupCreate);
		llContacts = (LinearLayout) findViewById(R.id.llContactsNewGroup);
		tvGroupName = (TextView) findViewById(R.id.tvNewGroupName);

		newMembers = new LinkedList<Messages.GroupMember>();

		results = new Intent();

		tvGroupName.setText(getIntent().getStringExtra("group_name"));

		contactsDBA.open();
		Cursor contacts = contactsDBA.getContacts();
		contacts.moveToFirst();

		for (int j = 0; !contacts.isAfterLast(); contacts.moveToNext(), ++j) {
			CheckBox current = new CheckBox(this);
			current.setText(contacts
					.getString(ContactsDBAdapter.USER_NAME_COLUMN));
			current.setChecked(false);

			current.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView,
						boolean isChecked) {
					Messages.GroupMember tmp = new Messages.GroupMember(
							buttonView.getText().toString(),
							Messages.GroupMember.StatusType.REQUESTED,
							Messages.GroupMember.RollType.USER);
					if (isChecked)
						newMembers.add(tmp);
					else
						newMembers.remove(tmp);
				}
			});

			llContacts.addView(current, j, new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));
		}
		contactsDBA.close();

		bCreateNewGroup.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				GroupDBAdapter groupDBA = GroupDBAdapter.getGroupDBA(getApplicationContext());
				SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
				groupDBA.insertEntry(tvGroupName.getText().toString(), settings.getString("user", ""), newMembers, getApplicationContext());
				finish();
			}
		});

		bCancelNewGroup.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

}
